#include "TinyTCPServer.h"
#include "..\TinyVoiceCardServer\TinyVoiceCardServerDefine.h"
#include "..\TinyVoiceCardServer\TinyTCPIF.h"
#include "TinySanhuiLibDefine.h"
#include "DeviceStateConnected.h"
#include "Device.h"


namespace DeviceStates {
CDeviceStateConnected::CDeviceStateConnected(void)
{
}

CDeviceStateConnected::~CDeviceStateConnected(void)
{
}


void CDeviceStateConnected::ProcessReceivedMsg(CTinyMsg &inMsg,CTinyMsg &OutMsg)
{
	switch(inMsg.getMsgIndex())
	{
	case VOICECARD_ACTION:
		switch(inMsg.getMsgItem())
		{
		case VOICECARD_PICKUP:
			{
				//pickup and upload connected msg
				std::string moduleName,deviceType,deviceID;

				inMsg.getDefaultParamByIndex(VOICECARD_PICKUP_MODULENAME,moduleName);
				inMsg.getDefaultParamByIndex(VOICECARD_PICKUP_DEVICEID,deviceID);
				inMsg.getDefaultParamByIndex(VOICECARD_PICKUP_DEVICETYPE,deviceType);

				if(this->deviceHandle->pickup() == true)
				{
					g_Logger.log(debug1,"call picked up from "+deviceID);
					//we should turn to dialout state for ivr dial
					//changeState();
					//g_TinyTCPIF.RaiseAlertEvt();
				}
				else
				{
					g_Logger.log(debug1,"call pickup failed"+deviceID);
				}
			}break;
		default:
			break;
		};
		break;
	};
}

int CDeviceStateConnected::CardCallBackProc(WORD wEvent, DWORD dwParam)
{
	switch(wEvent) 
	{
	case E_SYS_TIMEOUT://Timer event
		break;
	case E_CHG_ChState:
		{
			switch(int(dwParam&0xFFFF))
			{
				//call start
			case S_CALL_RINGING:
				{
					char buff[24];
					//up call alert event and then decide pickup
					/*if(0 == SsmPickup(deviceHandle->getChannelID()))
					SsmGetCallerId(deviceHandle->getChannelID(),buff);
					else
					//connected failed.*/

					g_TinyTCPIF.RaiseAlertEvt(deviceHandle->getDeviceID(),"Trunk");
					//should be convert to ringing state
					changeState(DEVICE_STATE_RINGING);

				}
				break;
			};
		}

	default:
		break;
	};
	return 0;
}
}
